<template>
  <el-drawer
    :visible.sync="drawerVisible"
    title="标签发布审核"
    size="60%"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="beforeClose"
    custom-class="tag-drawer tag-audit-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <div class="content-divider first">
          <div class="clearfix">
            <div class="number">01</div>
            <div class="divider-title">审核项明细<br />Audit item details</div>
            <div class="bg"></div>
          </div>
        </div>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="audit-item">
              <div class="item-title linear-blue">审核项名称</div>
              <div class="item-content disabled">{{ editForm.auditName }}</div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="audit-item">
              <div class="item-title linear-blue">审核项类别</div>
              <div class="item-content disabled">{{ $dict('$audit_type')[editForm.auditType] }}</div>
            </div>
          </el-col>
        </el-row>
        <audit-calculate v-if="editForm.auditType === '10'" :auditDetail="auditDetail"></audit-calculate>
        <audit-factory
          ref="auditFactory"
          v-else-if="editForm.auditType === '20'"
          :auditDetail="auditDetail"
        ></audit-factory>
        <div class="content-divider second">
          <div class="clearfix">
            <div class="number">02</div>
            <div class="divider-title">审核状态<br />Audit status</div>
            <div class="bg"></div>
          </div>
        </div>
        <div class="audit-box">
          <div class="audit-title linear-orange">
            <span class="icon"><icon-svg name="iconArts"></icon-svg></span><br />
            状态列表
          </div>
          <div class="audit-content audit-text">
            <tag-table ref="table" :maxHeight="165" :data="auditStatusInfoList" :columns="columns" :pagination="false">
            </tag-table>
          </div>
        </div>
        <template v-if="editable">
          <div class="content-divider third">
            <div class="clearfix">
              <div class="number">03</div>
              <div class="divider-title">审核反馈<br />Audit feedback</div>
              <div class="bg"></div>
            </div>
          </div>
          <el-form :model="editForm" label-width="0" ref="editForm" class="editForm">
            <el-form-item prop="auditStatus">
              <div class="audit-item">
                <div class="item-title linear-purpose">审核反馈</div>
                <div class="item-content">
                  <el-radio-group v-model="editForm.auditStatus">
                    <el-radio :label="1">同意</el-radio>
                    <el-radio :label="0">驳回</el-radio>
                  </el-radio-group>
                </div>
              </div>
            </el-form-item>
            <el-form-item prop="remarks">
              <div class="audit-box">
                <div class="audit-title linear-green">
                  <span class="icon"><icon-svg name="iconArts"></icon-svg></span><br />
                  审核意见
                </div>
                <div class="audit-content audit-text">
                  <el-input
                    type="textarea"
                    placeholder="请输入审核意见"
                    v-model="editForm.remarks"
                    :rows="7"
                    resize="none"
                  ></el-input>
                </div>
              </div>
            </el-form-item>
          </el-form>
        </template>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="beforeClose()">关闭</el-button>
      <el-button type="primary" @click="submit" v-if="editable">提交</el-button>
    </div>
  </el-drawer>
</template>

<script>
import auditCalculate from './components/audit-calculate'
import auditFactory from './components/audit-factory'
export default {
  components: {
    auditCalculate,
    auditFactory
  },
  data() {
    return {
      drawerVisible: false,
      editable: true,
      editForm: {
        auditId: '',
        tagId: '',
        auditName: '',
        auditType: '',
        auditStatus: 1,
        remarks: ''
      },
      auditDetail: {},
      auditStatusInfoList: [],
      columns: [
        {
          show: true,
          label: '序号',
          prop: 'orderNumber',
          align: 'center'
        },
        {
          show: true,
          label: '审核方',
          prop: 'auditName',
          align: 'center'
        },
        {
          show: true,
          label: '审核状态',
          prop: 'auditStatus',
          dict: '$audit_status',
          align: 'center'
        },
        {
          show: true,
          label: '备注',
          prop: 'remarks',
          align: 'center'
        }
      ],
      editFormRules: {
        auditStatus: [{ required: true, message: '审核反馈必须选择', trigger: 'change' }]
      }
    }
  },
  methods: {
    showDrawer(params) {
      this.editForm.auditId = params.auditId
      this.editForm.tagId = params.tagId
      this.editForm.auditName = params.auditName
      this.editForm.auditType = params.auditType
      this.$ajax({
        url: this.$ajax.setUrl(`tag/tagAuditCenter/getAuditInfo/${params.auditId}`),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.auditDetail = data.data.auditDetailed
          this.auditStatusInfoList = data.data.auditStatusInfoList
          this.editable = !!data.data.isImplement
          this.$nextTick(() => {
            if (params.auditType === '20' && data.data.auditDetailed.deriveTagResult)
              this.$refs.auditFactory.refreshGrpflter()
          })
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
      this.drawerVisible = true
    },
    beforeClose() {
      this.editForm = {
        auditId: '',
        tagId: '',
        auditName: '',
        auditType: '',
        auditStatus: 1,
        remarks: ''
      }
      this.auditDetail = {}
      this.auditStatusInfoList = []
      this.drawerVisible = false
    },
    submit() {
      this.$confirm(
        `确定${this.editForm.auditStatus === 1 ? '同意' : '驳回'}审核项【${this.editForm.auditName}】？`
      ).then(() => {
        let messageBox = document.getElementsByClassName('el-message-box__wrapper')
        messageBox[0].focus() // 防止按空格键重复确定
        this.$ajax({
          url: this.$ajax.setUrl('tag/tagAuditCenter/isAgreeAudit'),
          method: 'post',
          data: this.$ajax.setData({
            auditId: this.editForm.auditId,
            auditStatus: this.editForm.auditStatus,
            remarks: this.editForm.remarks
          })
        }).then(({ data }) => {
          if (data && data.code === '0') {
            this.$message.success(data.msg)
            this.$emit('callback')
            this.beforeClose()
          } else {
            this.$message.error(data.data || data.msg)
          }
        })
      })
    }
  }
}
</script>

<style lang="scss">
@import './audit-drawer';
</style>
